package scau.gsmg.misp.utils;


import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

/**
 * @Title: FormAuthenticationCaptchaFilter.java
 * @Group: GaoShenMenGui
 * @Date: 2015-5-31
 * @description:添加验证码验证，扩展验证规则
 */
public class FormAuthenticationCaptchaFilter extends FormAuthenticationFilter {

	public static final String DEFAULT_CAPTCHA_PARAM = "captcha";
	private String captchaParam = DEFAULT_CAPTCHA_PARAM;

	public String getCaptchaParam() {
		return captchaParam;
	}

	protected String getCaptcha(ServletRequest request) {
		return WebUtils.getCleanParam(request, getCaptchaParam());
	}

	protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
		String username = getUsername(request);
		String password = getPassword(request);
		String captcha = getCaptcha(request);
		System.out.println("AuthenticationToken"+username+captcha);
		//boolean rememberMe = isRememberMe(request);
		//String host = getHost(request);
		if(captcha==null){
			captcha="WXY";
		}
		if(username==null){
			username="";
		}
		if(password==null){
			password="";
		}
		return new UsernamePasswordCaptchaToken(username,password.toCharArray(),captcha);
	}

}